package com.girant.picture.controller;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.girant.picture.annotation.AuthCheck;
import com.girant.picture.common.BaseResponse;
import com.girant.picture.common.ResultUtils;
import com.girant.picture.constant.UserConstant;
import com.girant.picture.domain.dto.user.*;
import com.girant.picture.domain.entity.User;
import com.girant.picture.domain.vo.LoginUserVO;
import com.girant.picture.domain.vo.UserVO;
import com.girant.picture.exception.ErrorCode;
import com.girant.picture.exception.ThrowUtils;
import com.girant.picture.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class MainController {

    private final UserService userService;
    @PostMapping("/register")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterReqDTO userRegisterRequest) {
        ThrowUtils.throwif(userRegisterRequest==null, ErrorCode.PARAMS_ERROR);
        String userAccount  = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassowrd();
        String checkPassword = userRegisterRequest.getCheckPassword();
       long account =  userService.userRegister(userAccount,userPassword,checkPassword);
       return ResultUtils.success(account);
    }

    @PostMapping("/login")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginReqDTO userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwif(userLoginRequest==null, ErrorCode.PARAMS_ERROR);
        String userAccount  = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassowrd();
        LoginUserVO loginUserVO = userService.userLogin(userAccount,userPassword,request);
        return ResultUtils.success(loginUserVO);
    }

    /**
     *获取当前登录用户
     */
    @PostMapping("/get/currentlogin")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
      User loginUser =  userService.getLoginUser(request);
        return ResultUtils.success(userService.getLoginUserVO(loginUser));
    }
    @PostMapping("/get/login")
    public BaseResponse<LoginUserVO> getUserList(HttpServletRequest request) {
        User loginUser =  userService.getLoginUser(request);
        return ResultUtils.success(userService.getLoginUserVO(loginUser));
    }

    /**
     * 用户注销
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }
    /**
     * 增加 用户
     */
    @PostMapping("/add")
    public BaseResponse<Long> userAdd(UserAddReqDTO request) {
        User user = new User();
        BeanUtil.copyProperties(request,user);
        String defaultPassword = "12345678";
        String encyptPassword = userService.getEncryptPassword(defaultPassword);
        user.setUserPassword(encyptPassword);
        userService.save(user);
        return ResultUtils.success(user.getId());
    }
    /**
     * 修改用户信息
     */
    @PostMapping("/undate")
    public BaseResponse<Boolean> userUpdate(UserUpdateReqDTO request) {
        User user = new User();
        BeanUtil.copyProperties(request,user);
        String encyptPassword = userService.getEncryptPassword(request.getPassword());
        user.setUserPassword(encyptPassword);
        boolean result =  userService.updateById(user);
        ThrowUtils.throwif(!result,ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(result);
    }

    /**
     * 删除用户信息
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> userDelete(UserUpdateReqDTO request) {
        User user = new User();
        BeanUtil.copyProperties(request,user);
        boolean result =  userService.removeById(user);
        return ResultUtils.success(result);
    }

    /**
     * 分页获取用户封装列表（仅管理员）
     *
     * @param userQueryRequest 查询请求参数
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> listUserVOByPage(@RequestBody UserQueryReqDTO userQueryRequest) {
        ThrowUtils.throwif(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        long current = userQueryRequest.getCurrent();
        long pageSize = userQueryRequest.getPageSize();
        Page<User> userPage = userService.page(new Page<>(current, pageSize),
                userService.getQueryWrapper(userQueryRequest));
        Page<UserVO> userVOPage = new Page<>(current, pageSize, userPage.getTotal());
        List<UserVO> userVOList = userService.getUserListVO(userPage.getRecords());
        userVOPage.setRecords(userVOList);
        return ResultUtils.success(userVOPage);
    }
}